package ilearn.job.ilearnjobgrayversionjava17.buss.controller;

import feign.FeignException;
import ilearn.job.ilearnjobgrayversionjava17.buss.feign.OrderFeigh;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.nio.ByteBuffer;
import java.util.Optional;

@Log4j2
@RestController
@RefreshScope
public class BussController {

    @Value(value = "${gray.version}")
    private String grayVersion ;
    @Autowired
    private OrderFeigh orderFeigh;

    @GetMapping("/getOrderInfo")
    public String getOrderInfo(@RequestParam("userId")String userId){
        String version = null;
        if(isGrayUser(userId)){
            version = grayVersion;
        }
        log.info("当前用户：{}，请求版本号：{}",userId,version);
        return orderFeigh.getOrderInfo(userId,version);
    }

    @GetMapping("/getOrderInfoByParmVersion")
    public String getOrderInfoByParmVersion(@RequestParam("userId")String userId){
        String version = null;
        if(isGrayUser(userId)){
            version = grayVersion;
        }
        log.info("当前用户：{}，请求版本号：{}",userId,version);
        return orderFeigh.getOrderInfoByParmVersion(userId,version);
    }

    @GetMapping("/getOrderInfoByPathVersion")
    public String getOrderInfoByPathVersion(@RequestParam("userId")String userId){
        String version = null;
        if(isGrayUser(userId)){
            version = grayVersion;
        }
        log.info("当前用户：{}，请求版本号：{}",userId,version);
        return orderFeigh.getOrderInfoByPathVersion(userId,version);
    }

    @GetMapping("/getOrderInfoByCookieVersion")
    public String getOrderInfoByCookieVersion(@RequestParam("userId")String userId){
        String version = null;
        if(isGrayUser(userId)){
            version = grayVersion;
        }
        log.info("当前用户：{}，请求版本号：{}",userId,version);
        return orderFeigh.getOrderInfoByCookieVersion(userId,version);
    }

    @ExceptionHandler({FeignException.class})
    @ResponseStatus
    public String feignExceptionHandler(FeignException feignException){
        log.info("调用外部服务出现异常",feignException);
        String serviceId = null;
        if(feignException.hasRequest()){
            log.info("请求报文:{}",feignException.contentUTF8());
            serviceId = feignException.request().url();
        }
        log.info("状态码:{}",feignException.status());
        Optional<ByteBuffer> optionalRsp = feignException.responseBody();
        if (optionalRsp.isPresent()){
            String rsp = new String(optionalRsp.get().array());
            log.info("第三方系统返回：{}",rsp);
            return "目标服务:" + serviceId +"\n第三方系统调用失败:" + rsp;
        }else {
            return "外部服务调用失败:" + serviceId + "\n返回信息:" + feignException.getMessage();
        }
    }

    private boolean isGrayUser(String userId){
        return userId.endsWith("_gray");
    }

}
